package com.he.hsdyc.service;

import com.he.hsdyc.mapper.UserMapper;
import com.he.hsdyc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class UserServiceDetail implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("收到的账号"+username);


        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        User info=userMapper.selectOneByExample(example);

        if (info==null){
            throw new RuntimeException("登录账号不存在");
        }
        System.out.println("查到的密码: "+info.getPassword());
        return info;
    }


    //集成springSecurity
    //1>pom加入jar包
    //2>加入security包下的所有配置类
    //3>加入CreateHttp,发送请求到security自带的/oauth/token请求token
    //4>继承UserDetailsService，加入自己的判断逻辑
    //5>继承TokenEnhancer，自定义自己的jwtToken
    //6>加入TokenUtils,项目中想要获取登录信息，可以这样获取：User user = TokenUtils.analysisToken(request);
    //7>postman测试时需要权限的接口需要在请求头中加入Bearer Token
    //8>MyResourceServerConfig可以配置需要权限过滤的路径
}
